package com.monkey_pig.memoryalbum.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.monkey_pig.memoryalbum.R;
import com.monkey_pig.memoryalbum.activity.adapter.MyExpandAdapter;
import com.monkey_pig.memoryalbum.activity.entity.ChildEntity;
import com.monkey_pig.memoryalbum.activity.entity.ParentEntity;
import com.monkey_pig.memoryalbum.common.AppContext;
import com.monkey_pig.memoryalbum.utils.L;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link AllPhotoFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link AllPhotoFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AllPhotoFragment extends Fragment {

    private MyExpandAdapter statusAdapter;

    @Bind(R.id.expandableListView)
    ExpandableListView expandableListView;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_all_photo, container, false);
        //使用黄油刀注入布局中的view
        ButterKnife.bind(this, view);
        //初始化ExpandListView
        initExpandListView();
        //testtext.setText((String) SPUtils.get(AppContext.getContext(),"Birthday","String"));
        return view;
    }


    /**
     * 初始化
     */
    private void initExpandListView() {
        statusAdapter = new MyExpandAdapter(AppContext.getContext(), getListData());
        expandableListView.setAdapter(statusAdapter);
        // 去掉默认带的箭头
        expandableListView.setGroupIndicator(null);
        // 设置默认选中项
        expandableListView.setSelection(0);

        // 遍历所有group,将所有项设置成默认展开
        int groupCount = expandableListView.getCount();
        for (int i = 0; i < groupCount; i++) {
            expandableListView.expandGroup(i);
        }
        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return false;
            }
        });
    }

    private List<ParentEntity> getListData() {
        List<ParentEntity> groupList;
        String[] strArray = new String[] { "1月5日", "1月2日","12月25日","12月22日","12月20日"};

        //实例化一个list组，其中包含了item的实例
        groupList = new ArrayList<ParentEntity>();
//        使用for循环 将一级item与二级item添加进去
        for (int i = 0; i < strArray.length; i++) {
            ParentEntity parentEntity = new ParentEntity();
            parentEntity.setGroupName(strArray[i]);
            //构建一级item实例 并且将二级item传入一级实例中
            List<ChildEntity> hashMapList = new ArrayList<ChildEntity>();

            //测试数据
            for (int j = 0; j < 8; j++) {
                hashMapList.add(getGridViewItem(j));
            }
            parentEntity.setChildList(hashMapList);
            L.d("二级item数量：",String.valueOf(hashMapList.size()));
            groupList.add(parentEntity);
        }
        return groupList;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public ChildEntity getGridViewItem(int page) {

        Bitmap tempBitmap = BitmapFactory.decodeResource(AppContext.getContext().getResources(), R.drawable.ic_launcher);

        ChildEntity tempItem = new ChildEntity(tempBitmap);

        return tempItem;
    }

}
